package gw.com.android.ui.trade.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppContances;
import gw.com.android.model.ConfigType;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.ui.trade.TradeRangeModel;
import gw.com.android.ui.views.OrderBottomView;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.android.utils.MobclickEventUtlis;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.app.Logger;
import www.com.library.app.PushMsgTabFragment;
import www.com.library.model.DataItemDetail;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.util.DoubleConverter;
import www.com.library.util.JsonUtil;
import www.com.library.util.NetworkMonitor;
import www.com.library.view.PriceTextView;

/**
 * 
 * @author Administrator
 *
 */
public abstract class BaseOrderFragment extends PushMsgTabFragment implements OnFocusChangeListener{

	/** 卖价layout */
	@Nullable
	@BindView(R.id.sell_price_layout)
	View m_sellLayout = null;
	/** 买价layout */
	@Nullable
	@BindView(R.id.buy_price_layout)
	View m_buyLayout = null;
	/** 买价控件 */
	@Nullable
	@BindView(R.id.current_ask_price)
	PriceTextView m_buypriceView = null;
	/** 卖价控件 */
	@Nullable
	@BindView(R.id.current_bid_price)
	PriceTextView m_sellpriceView = null;
	/**底部止损止盈视图*/
	@Nullable
	@BindView(R.id.bottom_view)
	OrderBottomView mBottomview;
	@Nullable
	@BindView(R.id.btn_custom_confirm)
	TextView m_ConfirmButton;
	@Nullable
	@BindView(R.id.order_content_layout)
	View mContentLayout;

	public int mUiCode; //产品code
	public String mSymbol;//产品名
	public int zoneId;//产品区域
	public int mTradeDir = AppContances.TRADE_TYPE_BUY; //买卖方向
	public String mCurLot = "";//当前手数
	/**当前的下单类型*/
	public int cmd;
	/**当前的下单编号，唯一标识*/
	public int mSeq = 0;
	/**下单数据存放对象*/
	public DataItemDetail mDetail = new DataItemDetail();
	/**下单状态监听对象*/
	public onOrderStatusListener mStatusListener;

	/**报价对象*/
	public DataItemDetail mTickModel = null;
	/**持仓或者挂单对象*/
	public DataItemDetail mPositionModel = null;
	/**产品对象*/
	public DataItemDetail mSymbolModel = new DataItemDetail();

	/**止损止盈范围值*/
	public TradeRangeModel mRangeModel;
	/**下单参数保存对象*/
	public JSONObject jsonData ;
	
	/**
	 * 设置下单状态监听事件
	 * @param listener
	 */
	public void setOrderStatusListener(onOrderStatusListener listener){
		mStatusListener = listener;
	}
	/**
	 * 此方法发生数据请求操作
	 */
	public abstract void requstData();
	
	/**
	 * 此方法判断界面元素，是否可以进行下单操作
	 * @return
	 */
	public abstract boolean hasPostData();

	public abstract void onOrderSuccessNotify(int notifyId, int orderId, DataItemDetail model);

	/**
	 * 设置下单后数据
	 */
	public DataItemDetail getItemDetail(){
		return mDetail;
	};

	public void onSymbolNotify(DataItemDetail tickModel){};

	public void onMarginNotify(int mSwitch){};

	/**
	 * 刷新产品数据对象
	 */
	public void updateSymbolModel(){
		try {
			String jsonStr = AppTerminal.instance().getTradeParamModel(mUiCode);
			Logger.e("jsonStr == " + jsonStr);
			if(JsonUtil.isJsonData(jsonStr)){
				JSONObject mArray = new JSONObject(jsonStr);
				mSymbolModel.clear();
				JsonUtil.toDataItemDetail(mSymbolModel, mArray);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 刷新界面价格数据
	 */
	public void updateWithPrice() {
		if(null != mTickModel){
			m_buypriceView.updateWithPrice(mTickModel.getString(GTSConst.JSON_KEY_BUYPRICE), mTickModel.getString(GTSConst.JSON_KEY_ZOOMRULE), false);
			ColorThemeUtil.instance().setPriceColor(m_buypriceView, mTickModel.getInt(GTSConst.JSON_KEY_BUYPRICESTATE), mTickModel.getInt(GTSConst.JSON_KEY_BUYPRICEARRAW), false);
			m_sellpriceView.updateWithPrice(mTickModel.getString(GTSConst.JSON_KEY_SELLPRICE), mTickModel.getString(GTSConst.JSON_KEY_ZOOMRULE), false);
			ColorThemeUtil.instance().setPriceColor(m_sellpriceView, mTickModel.getInt(GTSConst.JSON_KEY_SELLPRICESTATE), mTickModel.getInt(GTSConst.JSON_KEY_SELLPRICEARRAW), true);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		jsonData = new JSONObject();
	}

	@Override
	protected void initLayoutView() {
		ButterKnife.bind(this, mRootView);
		if(null != mContentLayout){
			mContentLayout.setMinimumHeight(minHeight());
		}
	}

	/**
	 * 返回下单界面的最小高度
	 * @return
	 */
	public int minHeight(){
		int titleH = (int)(getActivity().getResources().getDimension(R.dimen.height_a)
				+ getActivity().getResources().getDimension(R.dimen.space_b));
		int height = DeviceUtil.instance().getScreenPixelsHeight(getActivity())
				- DeviceUtil.instance().getDimenHeight(getActivity()) - titleH;
		return height;
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(null != mStatusListener){
			mStatusListener.onShowView();
		}
	}

	/**
	 * 提交订单
	 */
	@OnClick(R.id.btn_custom_confirm)
	void submitTrade(){
		if(CommonUtils.isFastDoubleClick()){
			return;
		}
		Logger.i("下单响应按钮事件");
		// 未连上网络时,气泡提示
		if (!NetworkMonitor.hasNetWork()) {
			return;
		}
		if(hasPostData()){
			if(null != mStatusListener){
				mStatusListener.onBegin();
			}
			requstData();
			MobclickEventUtlis.MobclickEventByAccountType(getActivity(),"Order_Submit");
//				new Timer().schedule(new TimerTask() {
//
//					@Override
//					public void run() {
//						requstData();
//					}
//				}, 8000);
		}
	}

	public void onOrderFailNotify(int notifyId, int resultCode) {
		Logger.e("onOrderFailNotify 下单失败处理逻辑 notifyId = " + notifyId +", resultCode = " + resultCode);
		if(mSeq == notifyId && null != mStatusListener && resultCode != 0){
			if(resultCode == 1){
				mDetail.setIntValue("failCode", DoubleConverter.toIntData(ConfigType.SERVER_ERROR_1016));
			}else{
				mDetail.setIntValue("failCode", resultCode);
			}
			mStatusListener.onFail(mDetail);
		}
//		else{
//			if(resultCode == 39){
//				Toast.makeText(getActivity(), ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1039), Toast.LENGTH_SHORT).show();
//			}
//		}
	}

	/**
	 * 下单状态逻辑处理
	 * @author Administrator
	 *
	 */
	public interface onOrderStatusListener{
		/**
		 * 下单开始，显示加载圈等操作
		 */
		void onBegin();
		/**
		 * 下单失败，返回失败错误号
		 * @param mDetail 失败数据对象
		 */
		void onFail(DataItemDetail mDetail);

		/**
		 * 产品不可用时的错误提示处理
		 */
		void onFail();
		/**
		 * 下单成功，返回成功数据
		 * @param mDetail 成功数据对象
		 */
		void onSuc(DataItemDetail mDetail);
		
		/**
		 * 下单界面错误检查，返回错误信息
		 * @param errorMessage 成功数据对象
		 */
		void onError(String errorMessage);
		
		/**
		 * 展示界面
		 */
		void onShowView();
	}
	
}
